package com.gams.device.print_jna;

import com.gams.common.DateUtils;
import com.sun.jna.Library;
import com.sun.jna.Native;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;

@Slf4j
public class PrintJna
{
    public interface CLibrary extends Library
    {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary("Msprintsdk-x64.dll", CLibrary.class);

        int SetUsbportauto();

        int SetInit();

        int PrintSelfcheck();

        int PrintString(String strData, int iImme);

        int PrintFeedline(int iLine);

        int GetStatus();

        int GetStatusspecial();

        int PrintCutpaper(int iMode);

        int PrintTransmit(byte bData[], int iLen);

        /**
         * @param String strData：内容
         * @param int    iLmargin：左边距，取值0-27 单位mm
         * @param int    iMside：单位长度，即QR码大小，取值1-8(有些打印机型只支持1-4)
         * @param int    iRound：环绕模式，0立即打印(不混排)、1 环绕(混排，有些机型不支持)
         * @return
         */
        int PrintQrcode(String strData, int iLmargin, int iMside, int iRound);

        int SetClose();

        //打印换行
        int PrintChargeRow();

        int SetReadZKmode(int mode);

        //3.11 设置文本放大(SetSizetext)

        /**
         * int iHeight：放大高度，取值(1-8)
         * int iWidth：放大宽度，取值(1-8)
         *
         * @return
         */
        int SetSizetext(int iHeight, int iWidth);

        /**
         * int iLeftspace：字符间距，取值0-576，单位0.125mm
         *
         * @param iHeight
         * @return
         */
        int SetLeftmargin(int iLeftspace);

        int SetClean();

        int SetBold(int iBold);
    }

    public static void main(String[] args)
    {
        print();
    }

    public static void print()
    {
        String qrcodeUrl = "http://192.168.16.219:8083/ruoyi-socket/swagger-ui/index.html";
        String title = "深圳宝安港务中心欢迎您！";
        String userName = "胡*宏";
        String idcard = "430922********423X";
        String bussinessName = "业务办理";
        String queueNo = "A00001";
        String text = "取号后请在大厅等候区域休息等候，因大厅实行无语音叫号，请注意显示屏提示，过号请重新取号，谢谢！";
        printNumber(title, qrcodeUrl, userName, idcard, bussinessName, queueNo, text);
    }

    public static int printNumber(
            String title,
            String qrcodeUrl,
            String userName,
            String idcard,
            String bussinessName,
            String queueNo,
            String text)
    {
        try
        {
            CLibrary.INSTANCE.SetUsbportauto();
            CLibrary.INSTANCE.SetInit();
            //CLibrary.INSTANCE.PrintSelfcheck();
            CLibrary.INSTANCE.SetReadZKmode(0);
            byte[] bData = title.getBytes("GBK");
            CLibrary.INSTANCE.SetSizetext(2, 2);
            CLibrary.INSTANCE.PrintTransmit(bData, bData.length);
            CLibrary.INSTANCE.PrintChargeRow();

            CLibrary.INSTANCE.PrintFeedline(2);

            CLibrary.INSTANCE.PrintQrcode(qrcodeUrl, 19, 8, 0);
            CLibrary.INSTANCE.PrintChargeRow();

            CLibrary.INSTANCE.SetClean();
            CLibrary.INSTANCE.PrintFeedline(1);

            byte[] cardid = (userName + "  " + idcard).getBytes("GBK");
            CLibrary.INSTANCE.SetSizetext(1, 1);
            CLibrary.INSTANCE.SetLeftmargin(140);
            CLibrary.INSTANCE.PrintTransmit(cardid, cardid.length);
            CLibrary.INSTANCE.PrintChargeRow();

            CLibrary.INSTANCE.PrintFeedline(1);

            byte[] data5 = (bussinessName + " - " + queueNo).getBytes("GBK");
            CLibrary.INSTANCE.SetSizetext(2, 2);
            CLibrary.INSTANCE.SetLeftmargin(80);
            CLibrary.INSTANCE.SetBold(1);
            CLibrary.INSTANCE.PrintTransmit(data5, data5.length);
            CLibrary.INSTANCE.PrintChargeRow();

            CLibrary.INSTANCE.SetBold(0);
            CLibrary.INSTANCE.PrintFeedline(3);
            CLibrary.INSTANCE.SetLeftmargin(0);
            byte[] tips = "温馨提示：".getBytes("GBK");
            CLibrary.INSTANCE.SetSizetext(1, 1);
            CLibrary.INSTANCE.PrintTransmit(tips, tips.length);


            CLibrary.INSTANCE.PrintChargeRow();
            byte[] tips2 = text.getBytes("GBK");
            CLibrary.INSTANCE.PrintTransmit(tips2, tips2.length);

            CLibrary.INSTANCE.PrintChargeRow();

            CLibrary.INSTANCE.PrintFeedline(2);

            String getNumberTime = "取号时间：" + DateUtils.getTime();
            byte[] timeData = getNumberTime.getBytes("GBK");
            CLibrary.INSTANCE.SetLeftmargin(220);
            CLibrary.INSTANCE.PrintTransmit(timeData, timeData.length);


            CLibrary.INSTANCE.PrintChargeRow();
            CLibrary.INSTANCE.PrintFeedline(6);
            CLibrary.INSTANCE.PrintCutpaper(0);
            CLibrary.INSTANCE.SetClose();
            CLibrary.INSTANCE.SetClean();
            int status = CLibrary.INSTANCE.GetStatus();
            System.out.println(status);
            return status;
        } catch (UnsatisfiedLinkError e)
        {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        return 0;
    }
}
